import math
import os
from Utilities.AutoGetOperator.selectPackage import get_func

Operator_IVQ_ROFS=get_func(r'Operators/OperationOperators/OperatorIVQROF.py','Operator_IVQ_ROFS')

class WASPAS(Operator_IVQ_ROFS):
    def __init__(self,matrix,weight_list=[],q=3, x=0.5, gangliang=[]):
        '''
        function: MABAC决策方法
        :param matrix: 决策矩阵
        :param weight_list: 属性权重
        :param q: q值
        :param gangliang: 成本型或效益型表示，成本型用0表示，效益型用1表示
        '''
        super(WASPAS,self).__init__(q=q)
        if x > 1 or x < 0:
            self.x = 0.5
        else:
            self.x = x
        self.q=q
        self.matrix = matrix
        if len(weight_list):
            self.weight_list = weight_list
        else:
            self.weight_list = [1/len(matrix[0]) for i in range(len(matrix[0]))]
        # 检测是否传入纲量，若未传入，则全默认为效益型属性
        if len(gangliang):
            self.gangliang=gangliang
        else:
            self.gangliang=[1 for i in range(len(matrix[0]))]

    def getPositiveNegative(self, matrix):
        '''
        function: 获取没个方案中的最大最小值
        matirx: 决策矩阵
        :return: 最大最小值的向量
        '''

        scoreMatrix = [[self.getsco(j, self.q).getScore() for j in i] for i in self.matrix]
        temp = [[scoreMatrix[i][j] for i in range(len(scoreMatrix))] for j in range(len(scoreMatrix[0]))]
        # 获得最大值与最小值的下标
        max_index = [i.index(max(i)) for i in temp]
        min_index = [i.index(min(i)) for i in temp]
        positive = []
        negtive = []
        for mx, mn, j in zip(max_index, min_index, range(len(matrix[0]))):
            positive.append(self.matrix[mx][j])
            negtive.append(self.matrix[mn][j])

        return positive, negtive

    def getNormalizedMatrix(self, matrix, positive, negtive, q):
        '''
        获取正负理想解
        :param DecisionMatrix:
        :return:标准化之后的元素
        '''
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                if self.gangliang[j] == 1:
                    matrix[i][j] = self.divid(matrix[i][j], positive[j], q)
                else:
                    matrix[i][j] = self.divid(negtive[j], matrix[i][j], q)
        return matrix


    def getQ1Q2(self, normalizedMatrix, weight, q):
        '''
        求Q1和Q2
        :param NormalizedMatrix: 规范化矩阵
        :param weight: 权重
        :param q: q值
        :return:
        '''
        Q1, Q2 = [], []
        for i in range(len(normalizedMatrix)):
            s1 = self.kmulti(normalizedMatrix[i][0], weight[0], q)
            s2 = self.pow(normalizedMatrix[i][0], weight[0], q)
            for j in range(1, len(normalizedMatrix[0])):
                s1 = self.add(s1, self.kmulti(normalizedMatrix[i][j], weight[j], q), q)
                s2 = self.multi(s2, self.pow(normalizedMatrix[i][j], weight[j], q), q)

            Q1.append(s1)
            Q2.append(s2)

        return Q1, Q2

    def getQ(self, Q1, Q2, q, x):
        '''
        获取Q值
        :param Matrix:
        :param q:
        :return:
        '''
        Q = []
        for i in range(len(Q1)):
            Q.append(self.add(self.kmulti(Q1[i], x, q), self.kmulti(Q2[i], 1 - x, q)))

        return Q

    def getResult(self):

        positive, negtive = self.getPositiveNegative(self.matrix)
        normalizedMatrix = self.getNormalizedMatrix(self.matrix, positive, negtive, self.q)
        Q1, Q2 = self.getQ1Q2(normalizedMatrix, self.weight_list, self.q)
        Q = self.getQ(Q1, Q2, self.q, self.x)
        res = [self.getsco(j, self.q).getScore() for j in Q]
        res=[i/sum(res) for i in res]
        # print(res)
        return res
if __name__ == "__main__":
    li = [
        [([0.81539, 0.91785], [0.17128, 0.28772]), ([0.12374, 0.22949], [0.55729, 0.67911]),
         ([0.84635, 0.95753], [0.10662, 0.21768]), ([0.73433, 0.90407], [0.20174, 0.23691]), ],
        [([0.79202, 0.84776], [0.10917, 0.11366]), ([0.29818, 0.3131], [0.47785, 0.64139]),
         ([0.14316, 0.29861], [0.78271, 0.8032]), ([0.01997, 0.33824], [0.71369, 0.85874]), ],
        [([0.89511, 0.90987], [0.1782, 0.21547]), ([0.19584, 0.25518], [0.4043, 0.51519]),
         ([0.24707, 0.43495], [0.71766, 0.74854]), ([0.08338, 0.28006], [0.1259, 0.34247]), ],
        [([0.09222, 0.1264], [0.78533, 0.81673]), ([0.24999, 0.31601], [0.63687, 0.7492]),
         ([0.13707, 0.23335], [0.75133, 0.88383]), ([0.70681, 0.77778], [0.00605, 0.18418]), ],
        [([0.1876, 0.28807], [0.42117, 0.57172]), ([0.1311, 0.19959], [0.7644, 0.81178]),
         ([0.06145, 0.20426], [0.86783, 0.89045]), ([0.74275, 0.86356], [0.14231, 0.25652])]
    ]
    weight = [.2, .2, .3, .3, ]
    q = 3
    a = WASPAS(li, weight, q, 0.5)
    res = a.getResult()
    print(res)